using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using VirtualKeyboard.Contract;

namespace VirtualKeyboard.Service.Commands
{
    public class MediaMethodConfig
    {
        private static readonly Dictionary<string, string> DefaultShortcutKeyDictionary = new Dictionary<string, string>
                                        {
                                            {KeyMethods.Media.TogglePlayPause, "Space"},
                                            {KeyMethods.Media.Stop, "F4"},
                                            {KeyMethods.Media.MuteVolume, "M"},
                                            {KeyMethods.Media.IncreaseVolume, "Up"},
                                            {KeyMethods.Media.DecreaseVolume, "Down"},
                                            {KeyMethods.Media.NextTrack, "PageDown"},
                                            {KeyMethods.Media.PreviousTrack, "PageUp"},
                                            {KeyMethods.Media.FullScreen, "Enter"},
                                            {KeyMethods.Media.Forward, "Right"},
                                            {KeyMethods.Media.Backward, "Left"},
                                            {KeyMethods.Media.FastForward, "Control+Right"},
                                            {KeyMethods.Media.FastBackward, "Control+Left"},
                                            {KeyMethods.Media.VeryFastForward, "Alt+Right"},
                                            {KeyMethods.Media.VeryFastBackward, "Alt+Left"},
                                            {KeyMethods.Keyboard.Enter,"Enter"}
                                        };

        private static readonly MediaMethodConfig instance = new MediaMethodConfig();

        public static MediaMethodConfig Instance
        {
            get { return instance; }
        }

        private readonly Dictionary<string, string> shortcutKeyDictionary;
        private readonly Configuration configuration;
        private readonly VirtualKeyboardSection virtualKeyboardSection;
        private readonly object dataLock = new object();

        private MediaMethodConfig()
        {
            shortcutKeyDictionary = new Dictionary<string, string>(DefaultShortcutKeyDictionary);

            configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            virtualKeyboardSection = configuration.GetSection("virtualKeyboard") as VirtualKeyboardSection;
            if(virtualKeyboardSection == null)
            {
                virtualKeyboardSection = new VirtualKeyboardSection();
                foreach (var item in DefaultShortcutKeyDictionary)
                {
                    virtualKeyboardSection.ShortcutKeys.Add(new ShortcutKeyElement
                                                                {Name = item.Key, ShortcutKey = item.Value});
                }
                configuration.Save();
                return;
            }
            var shortcutKeys = virtualKeyboardSection.ShortcutKeys;
            foreach (var shortcutKeyEle in shortcutKeys)
            {
                shortcutKeyDictionary[shortcutKeyEle.Name] = shortcutKeyEle.ShortcutKey;
            }
        }

        public void UpdateKey(string method,string shortcutKey)
        {
            lock (dataLock)
            {
                shortcutKeyDictionary[method] = shortcutKey;
                virtualKeyboardSection.ShortcutKeys[method] = new ShortcutKeyElement { Name = method, ShortcutKey = shortcutKey }; 
            }
            configuration.Save();
        }

        public string Parse(string method)
        {
            lock (dataLock)
            {
                string result;
                return shortcutKeyDictionary.TryGetValue(method, out result) ? result : null; 
            }
        }
    }
}